<?php

namespace App\Api\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;

use App\Models\ES\User AS esUser;
use App\Models\ES\Media AS esMedia;
use App\Services\ElasticService;
 
/**
 * Elasticsearch
 * Class EsearchController
 * @package App\Api\Controllers
 */
class EsearchController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        // 获取用户基本信息
        $this->getUserInfo(false);
    }

    public function createUsers(){
    	$esUser  = new esUser();
    	$info = $esUser->create();
    	return json_success('ok', $info, 10000);
    }
  		
    public function createMedia(){
    	$esMedia  = new esMedia();
    	$info = $esMedia->create();
    	return json_success('ok', $info, 10000);
    }

    

  	public function add(){
  		$type = (int)IT('type',1);
  		switch ($type) {
  			case 1:
  				$info = ElasticService::addUser();
  				break;
  			case 2:
  				$info = ElasticService::addMedia();
  				break;
  		}
  		return is_string($info) ? json_error($info) : json_success('ok');
  	}

  	public function getEslist(){
    	$type = (int)IT('type',-1);
    	$list = ["user"=>[],'media'=>[]];
    	switch ($type) {
    		case 1:
    			$s = ElasticService::getUserlist();
    			if(is_string($s)){
    				return json_error($s);
    			}
                $list['user'] = $s;
                $list['media'] = ['next_page'=>0, 'list'=>null];
    			break;
    		case 2:
    			$s = ElasticService::getMedialist();
    			if(is_string($s)){
    				return json_error($s);
    			}
                $list['user'] = ['next_page'=>0, 'list'=>null];
                $list['media'] =$s;
    			break;
    		default:
    			$list['user'] = ElasticService::getUserlist();
    			$list['media'] = ElasticService::getMedialist();
    			break;
    	}
    	return json_success('ok', $list, 10000);
    }

    public function addUsers()
    {
    	$p = IT('p',true);

    	$s1 = $p * 5;
        $s2 = $s1 + 5;
        $rows = 100;

        $esUser  = new esUser();
        $j = 0;
        for ($i = $s1; $i < $s2; $i++) {
            $users = D('users')->select(['id',"name","mobile"])->offset($i*$rows)->limit($rows)->get();
            if (count($users)) {
                foreach ($users as $item) {
                    echo '-';
                    $params = [
                        'uid' => $item->id,
                        'username'  => $item->name,
                        'mobile' => $item->mobile
                    ];

                    $info = $esUser->index($params);
                    $j++;
                    // print_r($info);
                }
            }
        }
        print_r("插入成功".$j."条");
    }

    public function addMedias()
    {
    	$p = IT('p',true);

    	$s1 = $p * 5;
        $s2 = $s1 + 5;
        $rows = 100;

        $esMedia  = new esMedia();
        $j = 0;
        for ($i = $s1; $i < $s2; $i++) {
            $media = D('media')->select(['id as mid',"title","description","cover"])->offset($i*$rows)->limit($rows)->get();
            if (count($media)) {
                foreach ($media as $item) {
                	$params = [
                        'mid' => $item->mid,
                        'title'  => $item->title,
                        'description' => $item->description,
                        "cover" => $item->cover,
                    ];
                    $info = $esMedia->index($params);
                    $j++;
                    // print_r($info);
                }
            }
        }
        print_r("插入成功".$j."条");
    }

    

    

}
